<?php
/**
 * 收藏API
 *
 * 收藏service
 *
 * @author    zhouchao <zhouchao@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */
namespace App\Services\FavoritesService;

use App\Services\BaseService;
use App\Services\ProductsService\ProductTool;
use App\Services\UserService\UserTool;
use DB;

class FavoritesService extends BaseService
{
    protected $productTool;
    protected $userTool;

    public function __construct(ProductTool $productTool,UserTool $userTool)
    {
        $this->productTool = $productTool;
        $this->userTool = $userTool;
    }
    /**
     * 添加收藏
     * @param $user_id
     * @param $create_id
     * @return array
     */
    public function addCollect($user_id,$resource_id,$type = 'products'){
        $product = $this->productTool->exitProducts($user_id,$resource_id);
        if(empty($product)){
            $this->createResult('M11001',array('type'=>0));
        }
        //收藏的类型
        $app_id = $this->getAppIdByAppName($type);
        if($app_id == false){
            return $this->createResult('M11003',array('type'=>0));
        }
        $this->table = 'favorites';
        $this->where = array(array('app_id','=',$app_id),array('resource_id','=',$resource_id),array('user_id','=',$user_id));
        $this->select = array('state','favorites_id');
        $favorites_product = $this->getById();
        if(empty($favorites_product)){
            //从未收藏过
            $result = $this->save(array('app_id'=>$app_id,'user_id'=>$user_id,'resource_id'=>$resource_id,'state'=>1,'created_at'=>time(),'updated_at'=>time()));
            if(empty($result)){
                return $this->createResult('M00003',(object)array());
            }
            //增加收藏数量
            $this->productTool->addCollectNum($resource_id,1);
            return $this->createResult('M00000',array('type'=>1),'globalcodes','收藏');
        }else{
            $this->table = 'favorites';
            $this->where = array(array('favorites_id','=',$favorites_product->favorites_id));
            //判断当前是收藏状态还是取消收藏状态
            if($favorites_product->state == 1){
                //收藏状态
                $result = $this->update(array('state'=>2,'updated_at'=>time()));
                if(empty($result)){
                    return $this->createResult('M00003',(object)array());
                }
                $this->productTool->minusCollectNum($resource_id,1);
                return $this->createResult('M00000',array('type'=>0),'globalcodes','取消');
            }else{
                $result = $this->update(array('state'=>1,'updated_at'=>time()));
                if(empty($result)){
                    return $this->createResult('M00003',(object)array());
                }
                $this->productTool->addCollectNum($resource_id,1);
                return $this->createResult('M00000',array('type'=>1),'globalcodes','收藏');
            }
        }
    }

    /**
     * 获取用户收藏
     * @param $user_id
     * @param int $per_page
     * @return array
     */
    public function getFavorites($user_id,$type = 'products',$per_page = 10){
        $app_id = $this->getAppIdByAppName($type);
        $this->setParameter(array(
            'table' => 'favorites',
            'where' => [['app_id','=',$app_id],['user_id','=',$user_id],['state','=',1]],
            'select'=> ['resource_id'],
            'order' => 'updated_at',
            'sort'  => 'desc'
        ));
        //获取用户的收藏
        $resource_ids = $this->getAllPaginated($per_page)->toArray();
        $products = array();
        foreach($resource_ids['data'] as $resource_id){
            $product = $this->productTool->get_favorite_products($resource_id->resource_id);
            if(!empty($product)){
                $products[] = $product;
            }
        }
        return $this->createResult('M00000',array('current_page'=>$resource_ids['current_page'],'per_page'=>$per_page,'total_page'=>$resource_ids['last_page'],'data'=>$products));
    }

    /**
     * 获取产品收藏用户列表
     */
    public function get_favorites_users($user_id,$resource_id,$app_id=3,$per_page = 10){
        $favorites_user_ids = $this->get_favorites_user_ids_by_product_id_Paginated($resource_id,$app_id,$per_page);
        $favorites_user_ids['data'] = $this->userTool->follows($favorites_user_ids['data'],$user_id);
        return $this->createResult('M00000',$favorites_user_ids);
    }

    /**
     * 根据product_id获取
     * @param $product_id
     * @return mixed
     */
    public function get_favorites_user_ids_by_product_id_Paginated($resource_id,$app_id = 3,$per_page = 10){
        $this->setParameter([
            'table' => 'favorites',
            'where' => [['app_id','=',$app_id],['resource_id','=',$resource_id],['state','=',1]],
            'select'=> ['user_id']
        ]);
        $favorites_user_ids = $this->getListByModuleId($per_page);
        return $favorites_user_ids;
    }

    /**
     * 根据product_id获取
     * @param $product_id
     * @return mixed
     */
    public function get_favorites_user_ids_by_product_id($resource_id,$app_id = 3,$per_page = 10){
        $favorites_user_ids = DB::table('favorites')
            ->where([['app_id','=',$app_id],['resource_id','=',$resource_id],['state','=',1]])
            ->take($per_page)
            ->pluck('user_id')
            ->toArray();
        return $favorites_user_ids;
    }
}